<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/4
 * Time: 1:22 AM
 */

namespace common\models;

use api\models\event\MissionEvent;
use api\models\queue\Sms;
use yii\base\Event;
use api\models\JobRecord;
use Yii;

/**
 * Class Mission
 * @package common\models
 *
 * @property int    $mission_type 任务类型
 * @property string $latitude 纬度
 * @property string $longitude 经度
 * @property string $ad_code 行政区划码
 * @property int    $gender_needed 性别要求
 * @property int    $start_at 任务开始时间
 * @property int    $end_at 任务结束时间
 * @property string $work_start 上班时间
 * @property string $work_end 下班时间
 * @property int    $salary 工资
 * @property int    $salary_type 工资类型
 * @property int    $pay_type 结算方式
 *
 */
class Mission extends Job
{
    const EVENT_AFTER_POST = "after_post";


    const PAY_TYPE_TAIL = 1; //收尾款
    const PAY_TYPE_FINESSES = 2; //完工结

    public $mission_type;
    public $latitude;
    public $longitude;
    public $ad_code;
    public $gender_needed;
    public $start_at;
    public $end_at;
    public $work_start;
    public $work_end;
    public $salary;
    public $pay_type;
    public $username;
    public $salary_type;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'type'], 'required'],
            [
                [
                    'uid',
                    'type',
                    'status',
                    'forbidden',
                    'pay_needed',
                    'created_at',
                    'updated_at',
                    'mission_type',
                    'gender_needed',
//                    'sign_end',
                    'salary',
                    'salary_type',
                    'pay_type',
                    'hide'
                ],
                'integer'
            ],
            [['content'], 'string'],
            [
                [
                    'title',
                    'contact_name',
                    'contact_phone',
//                    'color',
                    'latitude',
                    'longitude',
                    'ad_code',
                ],
                'string',
                'max' => 255
            ],
            [
                ['uid'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => User::className(),
                'targetAttribute' => ['uid' => 'id']
            ],
            [['work_start', 'work_end'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'            => 'ID',
            'uid'           => '发布者',
            'type'          => '类型',
            'status'        => '是否支付',
            'forbidden'     => '是否屏蔽',
            'pay_needed'    => '需要支付',
            'title'         => '任务名称',
            'created_at'    => '创建时间',
            'updated_at'    => '最后更新时间',
            'content'       => '任务详情',
            'contact_name'  => '联系人姓名',
            'contact_phone' => '联系人电话',
            'job_type'      => '兼职类型',
            'color'         => '文字颜色',
            'ad_code'       => '行政区划码',
            'gender_needed' => '性别要求',
            'sign_end'      => '报名结束时间',
            'work_start'    => '上班时间',
            'work_end'      => '下班时间',
            'salary'        => '总金额',
            'salary_type'   => '工资类型',
            'pay_type'      => '结算方式',
            'start_at'      => '任务开始时间',
            'end_at'        => '任务结束时间',
            'latitude'      => '经度',
            'longitude'     => '纬度',
            'mission_type'  => "任务类型",
            'view_num'      => "可看报名数",
        ];
    }


    public static function getMissionTypes()
    {
        $res   = [];
        $types = JobType::find()->where(['type' => JobType::TYPE_MISSION])->all();
        foreach ($types as $type) {
            $res[$type->id] = $type->name;
        }

        return $res;
    }


    public function afterFind()
    {
        $info                = $this->jobInfo;
        $this->mission_type  = $info->mission_type;
        $this->latitude      = $info->latitude;
        $this->longitude     = $info->longitude;
        $this->ad_code       = $info->ad_code;
        $this->gender_needed = $info->gender_needed;
        $this->start_at      = $info->start_at;
        $this->end_at        = $info->end_at;
        $this->work_end      = $info->work_end;
        $this->work_start    = $info->work_start;
        $this->salary        = $info->salary;
//        $this->salary_type   = $info->salary_type;
        $this->pay_type = $info->pay_typed;

        parent::afterFind();
    }

    public static function postMission($jid)
    {

        $mission = \api\models\Mission::findOne($jid);
        if ($mission != null) {
            $mission->hide      = Mission::ACCESS;
            $mission->status    = Mission::PAY_STATUS;
            $mission->forbidden = Mission::ACCESS;
            $event              = new MissionEvent();
            $event->mission     = $mission;
            if ($mission->save(false)) {
                $mission->trigger(Mission::EVENT_AFTER_POST, $event);

                return true;
            }
        }

        return false;

    }

    public static function takeMission($jid, $uid)
    {
        $record         = JobRecord::findOne(['jid' => $jid, 'uid' => $uid]);
        $record->status = JobRecord::STATUS_SIGN_UP;
        $record->save();
    }


    public static function signUpCharge($jid, Order $order)
    {
        $mission = Mission::findOne($jid);
        if ($mission != null) {
            $mission->view_num += ! empty($order->number) ? $order->number : 1;

            return $mission->save();
        }

        return false;
    }

    public static function afterPostHandler(MissionEvent $event)
    {
        /** @var Mission $mission */
//        $mission = $event->mission;
        $content = "会员专属提示:亲爱的会员，您关注的江湖任务已更新，请您前往查看，祝您财源滚滚。";
        $users   = User::getMissionNoticeUsers();
//        Yii::warning(count($users));
        foreach ($users as $user) {
            /**@var \common\models\User $user */
//            Yii::warning($user->username);
//            Yii::warning($user->phone);
            $id = Yii::$app->queue->delay(1)->push(new Sms([
                'phone'   => $user->phone,
                'message' => $content,
            ]));
//            Yii::warning(Yii::$app->queue->isDone($id));
            //todo:可以做任务监控
        }
    }

    public static function orderSmsNotice($user_id, Order $order)
    {
        //TODO:这个可以抽象成assign方法
        $good      = $order->good;
        $item_name = $good->right0->item_name;
    }
}